﻿using CRM.Authorization.Rule;
using CRM.Models;
using CRM.Services;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using CRM.Authorization.Permissions;
using System.Net.Http;
using Microsoft.Extensions.Logging;


namespace CRM.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class BuyerDataController : BaseController
    {
        private readonly MyDbContext _dbContext;
        private readonly BuyerDataService _service;
        private readonly IHttpClientFactory _httpClientFactory;
        private readonly ILogger<BuyerDataController> _logger;

        public BuyerDataController(MyDbContext dbContext, BuyerDataService service, IHttpClientFactory httpClientFactory, ILogger<BuyerDataController> logger)
        {
            _dbContext = dbContext;
            _service = service;
            _httpClientFactory = httpClientFactory;
            _logger = logger;
        }

        [HttpGet("[action]")]
        [Permission(BuyersData.MODULE)]
        public async Task<ResultStruct> BuyerIndex()
        {
            return Success(await _service.BuyerIndex());
        }

        [HttpGet("[action]")]
        [Permission(BuyersData.MODULE)]
        public async Task<ResultStruct> UserIndex()
        {
            return Success(await _service.UserIndex());
        }

        [HttpPost(nameof(Add))]
        public async Task<ResultStruct> Add(BuyerData vm)
        {
            await _service.Add(vm);
            return Success("添加成功");
        }

        [HttpPost(nameof(Edit))]
        public async Task<ResultStruct> Edit(BuyerData vm)
        {

            await _service.Edit(vm);
            return Success();
        }

        [HttpDelete("[action]/{id:int}")]
        public async Task<ResultStruct> DelInfo(int id)
        {
            var count = await _service.DelInfo(id);
            if (count == 1)
            {
                return Success("删除成功");
            }
            else
            {
                return Error("删除失败");
            }
        }

        [HttpPost("[action]/{id:int}")]
        public async Task<ResultStruct> Publish(int id)
        {
            var count = await _service.Publish(id);
            if (count == 1)
            {
                return Success();
            }
            else
            {
                return Error();
            }
        }
        [HttpPost("[action]/{id:int}")]
        public async Task<ResultStruct> Revoke(int id)
        {
            var count = await _service.Revoke(id);
            if (count == 1)
            {
                return Success();
            }
            else
            {
                return Error();
            }
        }

        [HttpGet("[action]")]
        public async Task<ResultStruct> BuyerDataLogIndex()
        {

            return Success(await _service.BuyerDataLogIndex());
        }

        [HttpGet("[action]")]
        public async Task<ResultStruct> VerfityIndex()
        {
            return Success(await _service.VerfityIndex());
        }
    }
}
